package com.zhoug.mpchart.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.Chart;
import com.zhoug.mpchart.R;
import com.zhoug.mpchart.ints.IMPChartPlaceholder;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

/**
 * 图表占位图绘制
 * @Author: zhoug
 * @Date: 2023/9/27
 * @Description:
 */
public class MPChartPlaceholderHelper implements IMPChartPlaceholder {
    private static final int IDLE = 0;
    private static final int EMPTY = 1;
    private static final int ERROR = 2;
    private int mPlaceholderStatus = IDLE;//占位图状态
    private Drawable placeholderImage;//占位图
    private int placeholderImageSize;//占位图大小
    private String placeholderTextTint;//占位图提示文本
    private int placeholderTextColor;//文本颜色
    private int placeholderTextSize;//文本大小
    private final TextPaint mTextPaint;
    private final Paint.FontMetrics mFontMetrics;
    private Chart<?> mChart;
    private Drawable emptyImage;
    private Drawable errorImage;
    private String emptyText;
    private String errorText;

    public MPChartPlaceholderHelper(@NonNull Chart<?> chart, Context context, AttributeSet attrs, int defStyle) {
        this.mChart = chart;
        mChart.setNoDataText(null);
        parseAttrs(context, attrs, defStyle);
        init();
        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(placeholderTextColor);
        mTextPaint.setTextSize(placeholderTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mFontMetrics = new Paint.FontMetrics();
    }

    public void parseAttrs(Context context, AttributeSet attrs, int defStyle) {
        if (defStyle == 0) {
            defStyle = R.attr.mp_ChardDefaultStyle;
        }
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MPChart, defStyle, R.style.MPChardDefaultStyle);
        placeholderImageSize = a.getDimensionPixelSize(R.styleable.MPChart_mp_placeholderImageSize, context.getResources().getDimensionPixelOffset(R.dimen.mp_placeholderImageSize));
        placeholderTextColor = a.getColor(R.styleable.MPChart_mp_placeholderTextColor, ContextCompat.getColor(context,R.color.mp_placeholderTextColor));
        placeholderTextSize = a.getDimensionPixelOffset(R.styleable.MPChart_mp_placeholderTextSize, context.getResources().getDimensionPixelOffset(R.dimen.mp_placeholderTextSize));
        emptyImage = a.getDrawable(R.styleable.MPChart_mp_emptyImage);
        errorImage = a.getDrawable(R.styleable.MPChart_mp_errorImage);
        emptyText = a.getString(R.styleable.MPChart_mp_emptyText);
        errorText = a.getString(R.styleable.MPChart_mp_errorText);
        a.recycle();
    }

    private void init() {
        if (mPlaceholderStatus == IDLE) {
            placeholderImage = null;
            placeholderTextTint = null;
        } else if (mPlaceholderStatus == EMPTY) {
            placeholderImage = emptyImage;
            placeholderTextTint =emptyText;
        } else if (mPlaceholderStatus == ERROR) {
            placeholderImage =errorImage;
            placeholderTextTint = errorText;
        }

    }

    /**
     * 暂无数据
     */
    @Override
    public void empty() {
        if (mPlaceholderStatus != EMPTY) {
            mPlaceholderStatus = EMPTY;
            init();
        }
        if (mChart != null) {
            mChart.setNoDataText(null);
            mChart.setData(null);
            mChart.notifyDataSetChanged();
            mChart.invalidate();
        }

    }

    /**
     * 数据加载失败
     */
    @Override
    public void error() {
        if (mPlaceholderStatus != ERROR) {
            mPlaceholderStatus = ERROR;
            init();
        }
        if (mChart != null) {
            mChart.setNoDataText(null);
            mChart.setData(null);
            mChart.notifyDataSetChanged();
            mChart.invalidate();
        }
    }

    /**
     * 重置占位图
     */
    @Override
    public void resetPlaceholder() {
        if (mPlaceholderStatus != IDLE) {
            mPlaceholderStatus = IDLE;
            init();
        }
        if (mChart != null) {
            mChart.notifyDataSetChanged();
            mChart.invalidate();
        }
    }


    /**
     * 绘制占位图
     *
     * @param canvas
     */
    @Override
    public void drawPlaceImage(Canvas canvas) {
        if (mChart == null || mChart.getData() != null || placeholderImage == null) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        MPChartUtils.setDrawableSize(placeholderImage, placeholderImageSize, placeholderImageSize);
        //计算图片居中需要的偏移量
        Rect bounds = placeholderImage.getBounds();
        int imgWidth = bounds.right - bounds.left;
        int imgHeight = bounds.bottom - bounds.top;
        int xOffset = bounds.left + width / 2 - (imgWidth) / 2;
        int yOffset = bounds.top + height / 2 - (imgHeight) / 2;
        //计算文本绘制在图表下面的偏移量
        boolean hasText = !TextUtils.isEmpty(placeholderTextTint);
        float textX = width >> 1;
        float textY = 0;
        if (hasText) {
            float v = mTextPaint.measureText(placeholderTextTint);
            mTextPaint.getFontMetrics(mFontMetrics);
            float textHeight = mFontMetrics.bottom - mFontMetrics.top;
            textX = imgWidth >> 1;
            textY = imgHeight + textHeight / 2;
        }

        canvas.save();
        canvas.translate(xOffset, yOffset);
        //绘制占位图
        placeholderImage.draw(canvas);
        //绘制文本
        if (hasText) {
            canvas.drawText(placeholderTextTint, textX, textY, mTextPaint);
        }
        canvas.restore();
    }

}
